package com.portico.dept.solarch

import grails.plugins.springsecurity.Secured

@grails.plugins.springsecurity.Secured(['ROLE_SA_USER','ROLE_SA_ADMIN'])
class ProjectForecastController {

  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  static scaffold = true

  def index = {
    redirect(action: "list", params: params)
  }

  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [projectForecastInstanceList: ProjectForecast.list(params), projectForecastInstanceTotal: ProjectForecast.count()]
  }

  def create = {
    def projectForecastInstance = new ProjectForecast()
    projectForecastInstance.properties = params
    return [projectForecastInstance: projectForecastInstance]
  }

  def save = {
    def projectForecastInstance = new ProjectForecast(params)
    if (projectForecastInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'projectForecast.label', default: 'ProjectForecast'), projectForecastInstance.id])}"
     // redirect(action: "show", id: projectForecastInstance.id)
        render (text: 'Save Complete... ')

    }
    else {
      render(view: "create", model: [projectForecastInstance: projectForecastInstance])
    }
  }

  def show = {
    def projectForecastInstance = ProjectForecast.get(params.id)
    if (!projectForecastInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectForecast.label', default: 'ProjectForecast'), params.id])}"
      redirect(action: "list")
    }
    else {
      [projectForecastInstance: projectForecastInstance]
    }
  }

  def edit = {
    def projectForecastInstance = ProjectForecast.get(params.id)
    if (!projectForecastInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectForecast.label', default: 'ProjectForecast'), params.id])}"
      redirect(action: "list")
    }
    else {
      return [projectForecastInstance: projectForecastInstance]
    }
  }

  def update = {
    def projectForecastInstance = ProjectForecast.get(params.id)
    if (projectForecastInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (projectForecastInstance.version > version) {

          projectForecastInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'projectForecast.label', default: 'ProjectForecast')] as Object[], "Another user has updated this ProjectForecast while you were editing")
          render(view: "edit", model: [projectForecastInstance: projectForecastInstance])
          return
        }
      }
      projectForecastInstance.properties = params
      if (!projectForecastInstance.hasErrors() && projectForecastInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'projectForecast.label', default: 'ProjectForecast'), projectForecastInstance.id])}"
        redirect(action: "show", id: projectForecastInstance.id)
      }
      else {
        render(view: "edit", model: [projectForecastInstance: projectForecastInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectForecast.label', default: 'ProjectForecast'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def projectForecastInstance = ProjectForecast.get(params.id)
    if (projectForecastInstance) {
      try {
        projectForecastInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'projectForecast.label', default: 'ProjectForecast'), params.id])}"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'projectForecast.label', default: 'ProjectForecast'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectForecast.label', default: 'ProjectForecast'), params.id])}"
      redirect(action: "list")
    }
  }
}
